<article id="wikiArticle">
<div> <div class="blockIndicator deprecated deprecatedHeader">
<p><strong><span class="icon-only-inline" title="This is an obsolete API and is no longer guaranteed to work."><i class="icon-trash"> </i></span> 已废弃</strong><br/>该特性已经从 Web 标准中删除，虽然一些浏览器目前仍然支持它，但也许会在未来的某个时间停止支持，请尽量不要使用该特性。</p>
</div></div>
<p><code>已废弃的<strong>compile</strong></code><strong><code>()</code></strong> 方法被用于在脚本执行过程中（重新）编译正则表达式。与<code>RegExp</code>构造函数基本一样。</p>
<h2 id="语法">语法</h2>
<pre><code class="language-javascript"><code><var>regexObj</var>.compile(<var>pattern, flags</var>)</code></code></pre>
<h3 id="参数">参数</h3>
<dl>
<dt><code>pattern</code></dt>
<dd>正则表达式的文本 。</dd>
<dt><code>flags</code></dt>
<dd>
<p>如果指定，标志可以具有以下值的任意组合：</p>
<dl>
<dt><code>g</code></dt>
<dd>全局匹配</dd>
<dt><code>i</code></dt>
<dd>忽略大小写</dd>
<dt><code>m</code></dt>
<dd>多行;让开始和结束字符（^ 和 $）工作在多行模式工作（例如，^ 和 $ 可以匹配字符串中每一行的开始和结束（行是由 \n 或 \r 分割的），而不只是整个输入字符串的最开始和最末尾处。</dd>
<dt><code>y</code></dt>
<dd>黏度; 在目标字符串中，只从正则表达式的lastIndex属性指定的显示位置开始匹配（并且不试图从任何之后的索引匹配）。</dd>
</dl>
</dd>
</dl>
<h2 id="描述">描述</h2>
<p><code>不推荐compile方法。你可以使用</code> <code>RegExp</code> 构造函数来得到相同效果。</p>
<h2 id="示例">示例</h2>
<h3 id="使用compile()"><code>使用compile()</code></h3>
<p>以下展示如何用新模式和新标志来重新编译正则表达式。</p>
<pre><code  class="language-javascript">var regexObj = new RegExp("foo", "gi"); 
regexObj.compile("new foo", "g");
</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-regexp.prototype.compile" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">RegExp.prototype.compile</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>Initial definition. Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers.</td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-regexp.prototype.compile" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">RegExp.prototype.compile</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td>Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers.</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
            This compatibility table still uses the old format,
            because we haven't yet converted the data it contains.
            <strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div></div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
</tr>
</tbody>
</table>
</div>
<h2 id="相关链接">相关链接</h2>
<ul>
<li><a href="Reference/RegExp" title="此页面仍未被本地化, 期待您的翻译!"><code>RegExp</code></a></li>
</ul>
</article>